package com.company.cloud.page.modules.app.utils;

import java.util.*;

import com.company.cloud.common.utils.FreeMarkerUtils;
import com.company.cloud.page.modules.api.models.PageComponentModel;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.company.cloud.common.bean.DataMapEntity;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.page.modules.app.models.PageDataRule;
import com.company.cloud.page.modules.app.models.PageElementRuleModel;

public class PageUtils {

	/**
	 * 读取页面配置的数据规则
	 * @param content:页面配置json
	 * @return 页面元素读取数据规则
	 * */
	public static List<PageElementRuleModel> getPageElementDataRule(String content) {
		List<PageElementRuleModel> data=new ArrayList<PageElementRuleModel>();
		JSONArray jsonArray= JSON.parseArray(content);
		if(jsonArray==null || jsonArray.size()<=0) {
			return data;
		}
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject obj=  jsonArray.getJSONObject(i);
			String key= obj.getString("key");
			String code= obj.getString("code");
			if(StringUtils.hasText(key)) {
				PageDataRule dataRule=obj.getObject("ds", PageDataRule.class);
				data.add(new PageElementRuleModel(key,code, dataRule));
			}else{
				data.add(new PageElementRuleModel(key,code));
			}
		}
		return data;
	}


	public static String changeContent(String content,Map<String, PageComponentModel>  componentMap){
		JSONArray jsonArray= JSON.parseArray(content);
		if(jsonArray==null || jsonArray.size()<=0) {
			return  content;
		}
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject obj=  jsonArray.getJSONObject(i);
			String code= obj.getString("code");
			JSONObject optionsData= obj.getJSONObject("optionsData");
			PageComponentModel componentConfig= componentMap.get(code);
			JSONObject componentObj= componentContent(componentConfig,optionsData);
			if(componentObj!=null){
				Set<String> componentKeySet= componentObj.keySet();
				for(String componentKey:componentKeySet){
					obj.put(componentKey,componentObj.getJSONObject(componentKey));
				}
				obj.remove("optionsData");
			}
		}
		return jsonArray.toJSONString();
	}



	private static JSONObject componentContent(PageComponentModel componentConfig,JSONObject optionsData){
		if(componentConfig==null){
			return null;
		}
		if(optionsData==null){
			return null;
		}
		String val= FreeMarkerUtils.renderString(componentConfig.getContent(),optionsData);
		System.out.println("FreeMarkerUtils:"+val);
		JSONObject componentObj= JSON.parseObject(val);
		return componentObj;
	}



	
	/**
	 * 页面设置数据
	 * @param content :页面配置json
	 * @param data : 数据
	 * @return 页面配置和数据
	 * */
	public static String setPageConfig2Data(String content,DataMapEntity data) {
		JSONArray jsonArray= JSON.parseArray(content);
		if(jsonArray==null || jsonArray.size()<=0) {
			return content;
		}
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject obj=  jsonArray.getJSONObject(i);
			String key= obj.getString("key");
			if(StringUtils.hasText(key)) {
				Object dataObj= data.get(key);
				if(dataObj!=null) {
					Map<String, Object> _eleData=new HashMap<String, Object>();
					_eleData.put("list", dataObj);
					obj.put("data", _eleData);
				}
			}
		}
		return jsonArray.toJSONString();
	}
	
	
	public static void main(String[] args) {
		String content="[{\"optionsData\":{\"mode\":0,\"name\":\"请设置姓名\",\"company\":\"请设置公司名称\",\"position\":\"职位\",\"wechat\":\"\",\"message\":\"\",\"tel\":\"\",\"share\":\"\",\"headImg\":\"\"},\"code\":\"card-box\"}]";
		Map<String, PageComponentModel>  componentMap=new HashMap<>();
		PageComponentModel p1=new PageComponentModel();
		p1.setCode("card-box");
		p1.setContent("{\"options\": {       \"mode\": \"${mode}\",       \"name\": \"${name}\",       \"company\": \"${company}\",       \"position\": \"${position}\",       \"wechat\": \"${wechat}\",       \"message\": \"${message}\",       \"tel\": \"${tel}\",       \"share\": \"${share}\",       \"headImg\": \"${headImg}\"     } }");
		componentMap.put("card-box",p1);
//
//
//
//		String val= PageUtils.changeContent(content,componentMap);
//		System.out.println("+++++++++++++++++++++++++++++++++++");
//		System.out.println(val);


		List<PageElementRuleModel> eleList = PageUtils.getPageElementDataRule(content);
		Set<String>  eleCodeSet=new HashSet<>();
		eleList.forEach(item->{
			System.out.println(JSON.toJSONString(item));
			if(StringUtils.hasText(item.getCode())){
				eleCodeSet.add(item.getCode());
			}
		});

		System.out.println(JSON.toJSONString(eleCodeSet));

//		Map<String, PageComponentModel>  componentMap= pageComponentCacheService.getPageComponentMapByCache(eleCodeSet);
		String content2= PageUtils.changeContent(content,componentMap);
		System.out.println(content2);
//		model.setContent(content);



//		List<PageElementRuleModel> list= PageUtils.getPageElementDataRule(content);
//		list.forEach(item->{
//			System.out.println(JSON.toJSONString(item));
//		});




	}
	
	
}
